package com.codeoss.module.stock.service.impl;

import java.util.List;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.codeoss.framework.service.impl.BaseService;
import com.codeoss.framework.util.DateUtil;
import com.codeoss.module.ir.service.IIrDocTypeService;
import com.codeoss.module.ir.service.IIrSequenceService;
import com.codeoss.module.stock.dao.StockMovementConfirmMapper;
import com.codeoss.module.stock.model.StockMovement;
import com.codeoss.module.stock.model.StockMovementConfirm;
import com.codeoss.module.stock.model.StockMovementConfirmLine;
import com.codeoss.module.stock.model.StockMovementLine;
import com.codeoss.module.stock.service.IStockMovementConfirmLineService;
import com.codeoss.module.stock.service.IStockMovementConfirmService;
import com.codeoss.module.stock.service.IStockMovementLineService;
import com.codeoss.module.stock.service.IStockMovementService;

@Service
public class StockMovementConfirmService extends BaseService<StockMovementConfirmMapper, StockMovementConfirm>
		implements IStockMovementConfirmService,ApplicationContextAware {
    private ApplicationContext applicationContext;
    @Autowired
	private IStockMovementConfirmLineService stockMovementConfirmLineService;
    @Autowired
	private IIrDocTypeService irDocTypeService;
    @Autowired
	private DateUtil dateUtil;
    @Autowired
	private IIrSequenceService irSequenceService;
	@Override
	public List<StockMovementConfirm> getConfirmByStockMovementId(Long stockMovementId) {
		// TODO Auto-generated method stub
		QueryWrapper<StockMovementConfirm> query=new QueryWrapper<StockMovementConfirm>();
		query.eq("stock_movement_id", stockMovementId);
		return list(query);
	}

	@Override
	@Transactional
	public StockMovementConfirm generateConfirm(Long stockMovementId) {
		// TODO Auto-generated method stub
		IStockMovementService stockMovementService=applicationContext.getBean(IStockMovementService.class);
		IStockMovementLineService stockMovementLineService=applicationContext.getBean(IStockMovementLineService.class);
		StockMovement stockMovement=stockMovementService.getById(stockMovementId);
		String docStatus=stockMovement.getDocStatus();
		if(!"DR".equals(docStatus)) {
			throw new RuntimeException("只有草稿状态 才能创建");
		}
		StockMovementConfirm stockMovementConfirm=createFromStockMovement(stockMovement);
		List<StockMovementLine> stockMovementLineList=stockMovementLineService.getLineListByStockMovementId(stockMovementId);
		for(int i=0;i<stockMovementLineList.size();i++) {
			StockMovementLine stockMovementLine=stockMovementLineList.get(i);
			stockMovementConfirmLineService.createConfirmLine(stockMovementConfirm, stockMovementLine);
		}
		return stockMovementConfirm;
	}
	
	@Transactional
	protected StockMovementConfirm createFromStockMovement(StockMovement stockMovement) {
		StockMovementConfirm stockMovementConfirm=new StockMovementConfirm();
		stockMovementConfirm.setStockMovementId(stockMovement.getId());
		stockMovementConfirm.setDocStatus("DR");
		Long docTypeId=irDocTypeService.getIrDocTypeIdByCode("stockmovementconfirm");
		stockMovementConfirm.setIrDocTypeId(docTypeId);
		stockMovementConfirm.setDateConfirmed(dateUtil.getSqlNowDatetime());
		stockMovementConfirm.setDocumentNo(irSequenceService.getNextSequenceNo("stockmovementconfirm"));
		insert(stockMovementConfirm);
		return stockMovementConfirm;
	}

	@Override
	public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
		// TODO Auto-generated method stub
		this.applicationContext=applicationContext;
	}

	@Override
	@Transactional
	public void prepareIt(Long id) {
		// TODO Auto-generated method stub
		List<StockMovementConfirmLine> lineList=stockMovementConfirmLineService.getLineListByStockMovementConfirmId(id);
		if(lineList==null ||lineList.size()==0) {
			throw new RuntimeException("没有明细");
		}
	}

	@Override
	@Transactional
	public void completeIt(Long id) {
		// TODO Auto-generated method stub
		StockMovementConfirm stockMovementConfirm=getById(id);
		stockMovementConfirm.setDocStatus("CO");
		update(stockMovementConfirm);
		if(!stockMovementConfirm.getApproved()) {
			return;
		}
		IStockMovementLineService stockMovementLineService=applicationContext.getBean(IStockMovementLineService.class);
		List<StockMovementConfirmLine> confirmLineList=stockMovementConfirmLineService.getLineListByStockMovementConfirmId(id);
		for(int i=0;i<confirmLineList.size();i++) {
			StockMovementConfirmLine stockMovementConfirmLine=confirmLineList.get(i);
			Long stockMovementLineId=stockMovementConfirmLine.getStockMovementLineId();
			StockMovementLine stockMovementLine=stockMovementLineService.getById(stockMovementLineId);
			stockMovementLine.setMovementQty(stockMovementConfirmLine.getConfirmedQty());
			stockMovementLineService.update(stockMovementLine);
		}
	}

	@Override
	@Transactional
	public void approveIt(Long id) {
		// TODO Auto-generated method stub
		StockMovementConfirm stockMovementConfirm=getById(id);
		stockMovementConfirm.setApproved(true);
		update(stockMovementConfirm);
	}

	@Override
	@Transactional
	public void rejectIt(Long id) {
		// TODO Auto-generated method stub
		StockMovementConfirm stockMovementConfirm=getById(id);
		stockMovementConfirm.setApproved(false);
		update(stockMovementConfirm);
	}

	@Override
	@Transactional
	public void setDocStatus(Long id, String docStatus) {
		// TODO Auto-generated method stub
		StockMovementConfirm stockMovementConfirm=getById(id);
		stockMovementConfirm.setDocStatus(docStatus);
		update(stockMovementConfirm);
	}
}